package ntu.sce.fyp.easilocation;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import ntu.sce.fyp.easilocation.datastore.DatastoreHelper;

public class CreateUserServlet extends HttpServlet {

	private static final long serialVersionUID = 453461660204634124L;
	private static final String homePage = "EasilocationWebApp.jsp";

	@Override
	public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//		UserService userService = UserServiceFactory.getUserService();
//        User user = userService.getCurrentUser();
//
//        if (user != null) {
//            resp.setContentType("text/plain");
//            resp.getWriter().println("Hello, " + user.getNickname());
//        } else {
//            resp.sendRedirect(userService.createLoginURL(req.getRequestURI()));
//        }
		
//		resp.setContentType("text/plain");
//		resp.getWriter().println("Hello, World.");
		resp.sendRedirect(homePage);
	}
	
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		resp.setContentType("text/xml");
		PrintWriter pw = resp.getWriter();
		
		/** retrieve username and password */
		String username = req.getParameter("username");
		String password = req.getParameter("password");
		String email = req.getParameter("email");
		String phone_number = req.getParameter("handphone");
		String birthday = req.getParameter("birthday");
		
		boolean flag = DatastoreHelper.INSTANCE.createUser(username, password, email, phone_number, birthday);
		if (flag) {
			pw.println("<create>");
			pw.println("	<status>SUCCESS</status>");
			pw.println("	<message>User create successfully</message>");
			pw.println("</create>");
		} else {
			pw.println("<create>");
			pw.println("	<status>ERROR</status>");
			pw.println("	<message>Cannot create user</message>");
			pw.println("</create>");
		}
	}
	
}
